package com.example.base.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname EnumToListConverter
 * @Description TODO
 * @Date 2023/9/25 9:53
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class EnumToListConverter {

    public static <T extends Enum<T>> List<Map<String, Object>> convert(Enum<T>[] enums, String... fieldNames) {
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            for (Enum<T> enm : enums) {
                Map<String, Object> map = new HashMap<>();
                for (String fieldName : fieldNames) {
                    Field field = enm.getDeclaringClass().getDeclaredField(fieldName);
                    field.setAccessible(true);
                    map.put(fieldName, field.get(enm));
                }
                list.add(map);
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return list;
    }
}
